package com.wufun.overseas.utils;

import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * Created by lala on 15/7/21.
 */
public class StringUtils {
    public static boolean isNotEmpty(String str) {
        return !(str == null || str.equals(""));
    }

    public static boolean isEmpty(String str) {
        return str == null || str.equals("");
    }

    public static boolean isNumeric(String code) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(code).matches();
    }

    public static String getSafeString(String str) {
        return str == null ? "" : str;
    }

    public static String getNameNoExt(String path) {
        int start = path.lastIndexOf('/');
        int end = path.lastIndexOf('.');
        if (end == -1)
            end = path.length();
        if (start == -1)
            return path.substring(0, end);
        else
            return path.substring(start + 1, end);
    }

    /**
     *  更换数字为有单位的短数
     * @param number
     * @return
     */
    public static String formatNumberShort(int number) {
        DecimalFormat df = new DecimalFormat("#");
        String fileSizeString = "";
        if (number < 10000) {
            fileSizeString = df.format((double) number) + "";
        } else   if (number < 1000000) {
            df = new DecimalFormat("##0.0");
            fileSizeString = df.format((double) number / 10000) + "万";
        } else    {
            fileSizeString = "100万+";
        }
        return fileSizeString;
    }

    /**
     *  更换数字为有单位的短数
     * @param number
     * @return
     */
    public static String formatNumberShort999(int number) {
        DecimalFormat df = new DecimalFormat("#");
        String fileSizeString = "";
        if (number < 10000) {
            fileSizeString = df.format((double) number) + "次安装";
        } else   if (number < 1000000) {
            df = new DecimalFormat("##0.0");
            fileSizeString = df.format((double) number / 10000) + "万次安装";
        } else if (number < 10000000)  {
            fileSizeString = df.format((double) number / 10000) + "万次安装";
        } else    {
            fileSizeString = "999万+次安装";
        }
        return fileSizeString;
    }
    /**
     *  更换数字为有单位的短数
     * @param number
     * @return
     */
    public static String formatNumberLiveCharm(long number) {
        DecimalFormat df = new DecimalFormat("#");
        String fileSizeString = "";
        if (number <= 999999) {
            fileSizeString = df.format((double) number) + "魅力";
        } else   if (number <= 99999999L) {
            df = new DecimalFormat("##0.0");
            fileSizeString = df.format((double) number / 10000) + "万魅力";
        }  else   if (number <= 99999999999L) {
            df = new DecimalFormat("##0.00");
            fileSizeString = df.format((double) number / 10000) + "亿魅力";
        } else    {
            fileSizeString = "1000+亿魅力";
        }
        return fileSizeString;
    }
}
